home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / ab20 / languags / cpp / gcgp1409.lha / src / gcc-1.40 / gcc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-27  |  54.0 KB  |  2,195 lines

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987,1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. This paragraph is here to try to keep Sun CC from dying.
  21. The number of chars here seems crucial!!!!  */
  22.  
  23. void record_temp_file ();
  24.  
  25. /* This program is the user interface to the C compiler and possibly to
  26. other compilers.  It is used because compilation is a complicated procedure
  27. which involves running several programs and passing temporary files between
  28. them, forwarding the users switches to those programs selectively,
  29. and deleting the temporary files at the end.
  30.  
  31. CC recognizes how to compile each input file by suffixes in the file names.
  32. Once it knows which kind of compilation to perform, the procedure for
  33. compilation is specified by a string called a "spec".
  34.  
  35. Specs are strings containing lines, each of which (if not blank)
  36. is made up of a program name, and arguments separated by spaces.
  37. The program name must be exact and start from root, since no path
  38. is searched and it is unreliable to depend on the current working directory.
  39. Redirection of input or output is not supported; the subprograms must
  40. accept filenames saying what files to read and write.
  41.  
  42. In addition, the specs can contain %-sequences to substitute variable text
  43. or for conditional text.  Here is a table of all defined %-sequences.
  44. Note that spaces are not generated automatically around the results of
  45. expanding these sequences; therefore, you can concatenate them together
  46. or with constant text in a single argument.
  47.  
  48.  %%    substitute one % into the program name or argument.
  49.  %i     substitute the name of the input file being processed.
  50.  %b     substitute the basename of the input file being processed.
  51.     This is the substring up to (and not including) the last period.
  52.  %g     substitute the temporary-file-name-base.  This is a string chosen
  53.     once per compilation.  Different temporary file names are made by
  54.     concatenation of constant strings on the end, as in `%g.s'.
  55.     %g also has the same effect of %d.
  56.  %d    marks the argument containing or following the %d as a
  57.     temporary file name, so that that file will be deleted if CC exits
  58.     successfully.  Unlike %g, this contributes no text to the argument.
  59.  %w    marks the argument containing or following the %w as the
  60.     "output file" of this compilation.  This puts the argument
  61.     into the sequence of arguments that %o will substitute later.
  62.  %W{...}
  63.     like %{...} but mark last argument supplied within
  64.     as a file to be deleted on failure.
  65.  %o    substitutes the names of all the output files, with spaces
  66.     automatically placed around them.  You should write spaces
  67.     around the %o as well or the results are undefined.
  68.     %o is for use in the specs for running the linker.
  69.     Input files whose names have no recognized suffix are not compiled
  70.     at all, but they are included among the output files, so they will
  71.     be linked.
  72.  %p    substitutes the standard macro predefinitions for the
  73.     current target machine.  Use this when running cpp.
  74.  %P    like %p, but puts `__' before and after the name of each macro.
  75.     This is for ANSI C.
  76.  %s     current argument is the name of a library or startup file of some sort.
  77.         Search for that file in a standard list of directories
  78.     and substitute the full pathname found.
  79.  %eSTR  Print STR as an error message.  STR is terminated by a newline.
  80.         Use this when inconsistent options are detected.
  81.  %a     process ASM_SPEC as a spec.
  82.         This allows config.h to specify part of the spec for running as.
  83.  %l     process LINK_SPEC as a spec.
  84.  %L     process LIB_SPEC as a spec.
  85.  %G     process LIBG_SPEC as a spec.  A capital G is actually used here.
  86.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  87.  %E     process ENDFILE_SPEC as a spec.  A capital E is actually used here.
  88.  %c    process SIGNED_CHAR_SPEC as a spec.
  89.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  90.  %1    process CC1_SPEC as a spec.
  91.  %{S}   substitutes the -S switch, if that switch was given to CC.
  92.     If that switch was not specified, this substitutes nothing.
  93.     Here S is a metasyntactic variable.
  94.  %{S*}  substitutes all the switches specified to CC whose names start
  95.     with -S.  This is used for -o, -D, -I, etc; switches that take
  96.     arguments.  CC considers `-o foo' as being one switch whose
  97.     name starts with `o'.  %{o*} would substitute this text,
  98.     including the space; thus, two arguments would be generated.
  99.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  100.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  101.  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
  102.  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
  103.  
  104. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  105. other nested % constructs or spaces, or even newlines.
  106. They are processed as usual, as described above.
  107.  
  108. The character | is used to indicate that a command should be piped to
  109. the following command, but only if -pipe is specified.
  110.  
  111. Note that it is built into CC which switches take arguments and which
  112. do not.  You might think it would be useful to generalize this to
  113. allow each compiler's spec to say which switches take arguments.  But
  114. this cannot be done in a consistent fashion.  CC cannot even decide
  115. which input files have been specified without knowing which switches
  116. take arguments, and it must know which input files to compile in order
  117. to tell which compilers to run.
  118.  
  119. CC also knows implicitly that arguments starting in `-l' are to
  120. be treated as compiler output files, and passed to the linker in their proper
  121. position among the other output files.
  122.  
  123. */
  124.  
  125. #include <stdio.h>
  126. #include <sys/types.h>
  127. #include <signal.h>
  128. #include <sys/file.h>
  129. #include <sys/stat.h>
  130.  
  131. #ifndef __STDC__
  132. extern char *sys_errlist[];
  133. #define strerror(err) sys_errlist[err]
  134. #endif
  135.  
  136. #include "config.h"
  137. #include "obstack.h"
  138. #include "gvarargs.h"
  139.  
  140. #ifdef USG
  141. #ifndef R_OK
  142. #define R_OK 4
  143. #define W_OK 2
  144. #define X_OK 1
  145. #endif
  146.  
  147. #define vfork fork
  148. #endif /* USG */
  149.  
  150. #define obstack_chunk_alloc xmalloc
  151. #define obstack_chunk_free free
  152. extern int xmalloc ();
  153. extern void free ();
  154.  
  155. /* If a stage of compilation returns an exit status >= 1,
  156.    compilation of that file ceases.  */
  157.  
  158. #define MIN_FATAL_STATUS 1
  159.  
  160. /* This is the obstack which we use to allocate many strings.  */
  161.  
  162. struct obstack obstack;
  163.  
  164. char *handle_braces ();
  165. char *save_string ();
  166. char *concat ();
  167. int do_spec ();
  168. int do_spec_1 ();
  169. char *find_file ();
  170. static char *find_exec_file ();
  171. void validate_switches ();
  172. void validate_all_switches ();
  173. void fancy_abort ();
  174.  
  175. /* config.h can define ASM_SPEC to provide extra args to the assembler
  176.    or extra switch-translations.  */
  177. #ifndef ASM_SPEC
  178. #define ASM_SPEC ""
  179. #endif
  180.  
  181. /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
  182.    or extra switch-translations.  */
  183. #ifndef CPP_SPEC
  184. #define CPP_SPEC ""
  185. #endif
  186.  
  187. /* config.h can define CC1_SPEC to provide extra args to cc1
  188.    or extra switch-translations.  */
  189. #ifndef CC1_SPEC
  190. #define CC1_SPEC ""
  191. #endif
  192.  
  193. /* config.h can define LINK_SPEC to provide extra args to the linker
  194.    or extra switch-translations.  */
  195. #ifndef LINK_SPEC
  196. #define LINK_SPEC ""
  197. #endif
  198.  
  199. /* config.h can define LIB_SPEC to override the default libraries.  */
  200. #ifndef LIB_SPEC
  201. #define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  202. #endif
  203.  
  204. /* config.h can define ENDFILE_SPEC to override the default crtn files.  */
  205. #ifndef ENDFILE_SPEC
  206. #define ENDFILE_SPEC ""
  207. #endif
  208.  
  209. /* config.h can define LIBG_SPEC to override the default debug libraries.  */
  210. #ifndef LIBG_SPEC
  211. #define LIBG_SPEC "%{g:-lg}"
  212. #endif
  213.  
  214. /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
  215. #ifndef STARTFILE_SPEC
  216. #define STARTFILE_SPEC  \
  217.   "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
  218. #endif
  219.  
  220. /* This spec is used for telling cpp whether char is signed or not.  */
  221. #define SIGNED_CHAR_SPEC  \
  222.   (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}"    \
  223.    : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
  224.  
  225. /* This defines which switch letters take arguments.  */
  226.  
  227. #ifndef SWITCH_TAKES_ARG
  228. #define SWITCH_TAKES_ARG(CHAR)      \
  229.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  230.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  231.    || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
  232.    || (CHAR) == 'L' || (CHAR) == 'i' || (CHAR) == 'A')
  233. #endif
  234.  
  235. /* This defines which multi-letter switches take arguments.  */
  236.  
  237. #ifndef WORD_SWITCH_TAKES_ARG
  238. #define WORD_SWITCH_TAKES_ARG(STR) (!strcmp (STR, "Tdata"))
  239. #endif
  240.  
  241. /* This structure says how to run one compiler, and when to do so.  */
  242.  
  243. struct compiler
  244. {
  245.   char *suffix;            /* Use this compiler for input files
  246.                    whose names end in this suffix.  */
  247.   char *spec;            /* To use this compiler, pass this spec
  248.                    to do_spec.  */
  249. };
  250.  
  251. /* Here are the specs for compiling files with various known suffixes.
  252.    A file that does not end in any of these suffixes will be passed
  253.    unchanged to the loader and nothing else will be done to it.  */
  254.  
  255. struct compiler compilers[] =
  256. {
  257.   {".c",
  258.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} %{trigraphs} -undef \
  259.         -D__GNUC__ %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!ansi:%p} %P\
  260.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic} %{P}\
  261.     %{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %C\
  262.         %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%W{o*}}%{M*:%W{o*}} |\n\
  263.     %{!M*:%{!E:cc1 %{!pipe:%g.cpp} %1 \
  264.            %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
  265.            %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  266.            %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  267.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  268.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  269.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  270.               %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  271.                       %{!pipe:%g.s}\n }}}"},
  272.   {".cc",
  273.    "cpp -+ %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} \
  274.         -undef -D__GNUC__ -D__GNUG__ %p %P\
  275.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic} %{P}\
  276.     %{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %C\
  277.         %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%W{o*}}%{M*:%W{o*}} |\n\
  278.     %{!M*:%{!E:cc1plus %{!pipe:%g.cpp} %1\
  279.            %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
  280.            %{g} %{O} %{W*} %{w} %{pedantic} %{traditional}\
  281.            %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  282.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  283.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  284.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  285.               %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  286.                       %{!pipe:%g.s}\n }}}"},
  287.   {".i",
  288.    "cc1 %i %1 %{!Q:-quiet} %{Y*} %{d*} %{m*} %{f*} %{a}\
  289.     %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  290.     %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  291.     %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  292.     %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  293.             %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s}\n }"},
  294.   {".s",
  295.    "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  296.             %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i\n }"},
  297.   {".S",
  298.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{trigraphs} \
  299.         -undef -D__GNUC__ -$ %p %P\
  300.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic} %{P}\
  301.     %{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %C\
  302.         %i %{!M*:%{!E:%{!pipe:%g.s}}}%{E:%W{o*}}%{M*:%W{o*}} |\n\
  303.     %{!M*:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  304.                     %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  305.             %{!pipe:%g.s}\n }}}"},
  306.   /* Mark end of table */
  307.   {0, 0}
  308. };
  309.  
  310. /* Here is the spec for running the linker, after compiling all files.  */
  311. char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\
  312.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  313.  %{y*} %{!A:%{!nostdlib:%S}} \
  314.  %{L*} %o %{!nostdlib:%G gnulib%s %L gnulib%s %{!A:%E}}\n }}}}";
  315.  
  316. /* Accumulate a command (program name and args), and run it.  */
  317.  
  318. /* Vector of pointers to arguments in the current line of specifications.  */
  319.  
  320. char **argbuf;
  321.  
  322. /* Number of elements allocated in argbuf.  */
  323.  
  324. int argbuf_length;
  325.  
  326. /* Number of elements in argbuf currently in use (containing args).  */
  327.  
  328. int argbuf_index;
  329.  
  330. /* Number of commands executed so far.  */
  331.  
  332. int execution_count;
  333.  
  334. /* Flag indicating whether we should print the command and arguments */
  335.  
  336. unsigned char vflag;
  337.  
  338. /* Name with which this program was invoked.  */
  339.  
  340. char *programname;
  341.  
  342. /* User-specified -B prefix to attach to command names,
  343.    or 0 if none specified.  */
  344.  
  345. char *user_exec_prefix = 0;
  346.  
  347. /* Environment-specified prefix to attach to command names,
  348.    or 0 if none specified.  */
  349.  
  350. char *env_exec_prefix = 0;
  351.  
  352. /* Suffix to attach to directories searched for commands.  */
  353.  
  354. char *machine_suffix = 0;
  355.  
  356. /* Default prefixes to attach to command names.  */
  357.  
  358. #ifndef STANDARD_EXEC_PREFIX
  359. #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"
  360. #endif /* !defined STANDARD_EXEC_PREFIX */
  361.  
  362. char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
  363. char *standard_exec_prefix_1 = "/usr/lib/gcc-";
  364.  
  365. #ifndef STANDARD_STARTFILE_PREFIX
  366. #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
  367. #endif /* !defined STANDARD_STARTFILE_PREFIX */
  368.  
  369. char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
  370. char *standard_startfile_prefix_1 = "/lib/";
  371. char *standard_startfile_prefix_2 = "/usr/lib/";
  372.  
  373. /* Clear out the vector of arguments (after a command is executed).  */
  374.  
  375. void
  376. clear_args ()
  377. {
  378.   argbuf_index = 0;
  379. }
  380.  
  381. /* Add one argument to the vector at the end.
  382.    This is done when a space is seen or at the end of the line.
  383.    If DELETE_ALWAYS is nonzero, the arg is a filename
  384.     and the file should be deleted eventually.
  385.    If DELETE_FAILURE is nonzero, the arg is a filename
  386.     and the file should be deleted if this compilation fails.  */
  387.  
  388. void
  389. store_arg (arg, delete_always, delete_failure)
  390.      char *arg;
  391.      int delete_always, delete_failure;
  392. {
  393.   if (argbuf_index + 1 == argbuf_length)
  394.     argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  395.  
  396.   argbuf[argbuf_index++] = arg;
  397.   argbuf[argbuf_index] = 0;
  398.  
  399.   if (delete_always || delete_failure)
  400.     record_temp_file (arg, delete_always, delete_failure);
  401. }
  402.  
  403. /* Record the names of temporary files we tell compilers to write,
  404.    and delete them at the end of the run.  */
  405.  
  406. /* This is the common prefix we use to make temp file names.
  407.    It is chosen once for each run of this program.
  408.    It is substituted into a spec by %g.
  409.    Thus, all temp file names contain this prefix.
  410.    In practice, all temp file names start with this prefix.
  411.  
  412.    This prefix comes from the envvar TMPDIR if it is defined;
  413.    otherwise, from the P_tmpdir macro if that is defined;
  414.    otherwise, in /usr/tmp or /tmp.  */
  415.  
  416. char *temp_filename;
  417.  
  418. /* Length of the prefix.  */
  419.  
  420. int temp_filename_length;
  421.  
  422. /* Define the list of temporary files to delete.  */
  423.  
  424. struct temp_file
  425. {
  426.   char *name;
  427.   struct temp_file *next;
  428. };
  429.  
  430. /* Queue of files to delete on success or failure of compilation.  */
  431. struct temp_file *always_delete_queue;
  432. /* Queue of files to delete on failure of compilation.  */
  433. struct temp_file *failure_delete_queue;
  434.  
  435. /* Record FILENAME as a file to be deleted automatically.
  436.    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
  437.    otherwise delete it in any case.
  438.    FAIL_DELETE nonzero means delete it if a compilation step fails;
  439.    otherwise delete it in any case.  */
  440.  
  441. void
  442. record_temp_file (filename, always_delete, fail_delete)
  443.      char *filename;
  444.      int always_delete;
  445.      int fail_delete;
  446. {
  447.   register char *name;
  448.   name = (char *) xmalloc (strlen (filename) + 1);
  449.   strcpy (name, filename);
  450.  
  451.   if (always_delete)
  452.     {
  453.       register struct temp_file *temp;
  454.       for (temp = always_delete_queue; temp; temp = temp->next)
  455.     if (! strcmp (name, temp->name))
  456.       goto already1;
  457.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  458.       temp->next = always_delete_queue;
  459.       temp->name = name;
  460.       always_delete_queue = temp;
  461.     already1:;
  462.     }
  463.  
  464.   if (fail_delete)
  465.     {
  466.       register struct temp_file *temp;
  467.       for (temp = failure_delete_queue; temp; temp = temp->next)
  468.     if (! strcmp (name, temp->name))
  469.       goto already2;
  470.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  471.       temp->next = failure_delete_queue;
  472.       temp->name = name;
  473.       failure_delete_queue = temp;
  474.     already2:;
  475.     }
  476. }
  477.  
  478. /* Delete all the temporary files whose names we previously recorded.  */
  479.  
  480. void
  481. delete_temp_files ()
  482. {
  483.   register struct temp_file *temp;
  484.  
  485.   for (temp = always_delete_queue; temp; temp = temp->next)
  486.     {
  487. #ifdef DEBUG
  488.       int i;
  489.       printf ("Delete %s? (y or n) ", temp->name);
  490.       fflush (stdout);
  491.       i = getchar ();
  492.       if (i != '\n')
  493.     while (getchar () != '\n') ;
  494.       if (i == 'y' || i == 'Y')
  495. #endif /* DEBUG */
  496.     {
  497.       struct stat st;
  498.       if (stat (temp->name, &st) >= 0)
  499.         {
  500.           /* Delete only ordinary files.  */
  501.           if ((st.st_mode & S_IFMT) == S_IFREG)
  502.         if (unlink (temp->name) < 0)
  503.           if (vflag)
  504.             perror_with_name (temp->name);
  505.         }
  506.     }
  507.     }
  508.  
  509.   always_delete_queue = 0;
  510. }
  511.  
  512. /* Delete all the files to be deleted on error.  */
  513.  
  514. void
  515. delete_failure_queue ()
  516. {
  517.   register struct temp_file *temp;
  518.  
  519.   for (temp = failure_delete_queue; temp; temp = temp->next)
  520.     {
  521. #ifdef DEBUG
  522.       int i;
  523.       printf ("Delete %s? (y or n) ", temp->name);
  524.       fflush (stdout);
  525.       i = getchar ();
  526.       if (i != '\n')
  527.     while (getchar () != '\n') ;
  528.       if (i == 'y' || i == 'Y')
  529. #endif /* DEBUG */
  530.     {
  531.       if (unlink (temp->name) < 0)
  532.         if (vflag)
  533.           perror_with_name (temp->name);
  534.     }
  535.     }
  536. }
  537.  
  538. void
  539. clear_failure_queue ()
  540. {
  541.   failure_delete_queue = 0;
  542. }
  543.  
  544. /* Compute a string to use as the base of all temporary file names.
  545.    It is substituted for %g.  */
  546.  
  547. void
  548. choose_temp_base ()
  549. {
  550.   extern char *getenv ();
  551.   char *base = getenv ("TMPDIR");
  552.   int len;
  553.  
  554.   if (base == (char *)0)
  555.     {
  556. #ifdef P_tmpdir
  557.       if (access (P_tmpdir, R_OK | W_OK) == 0)
  558.     base = P_tmpdir;
  559. #endif
  560.       if (base == (char *)0)
  561.     {
  562. #ifdef amigados
  563.       base = "ram:";
  564. #else
  565.       if (access ("/usr/tmp", R_OK | W_OK) == 0)
  566.         base = "/usr/tmp/";
  567.       else
  568.         base = "/tmp/";
  569. #endif
  570.     }
  571.     }
  572.  
  573.   len = strlen (base);
  574.   temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));
  575.   strcpy (temp_filename, base);
  576.   if (len > 0 && temp_filename[len-1] != '/'
  577. #ifdef amigados
  578.                         && temp_filename[len-1] != ':'
  579. #endif
  580.                                         )
  581.     temp_filename[len++] = '/';
  582.   strcpy (temp_filename + len, "ccXXXXXX");
  583.  
  584.   mktemp (temp_filename);
  585.   temp_filename_length = strlen (temp_filename);
  586. }
  587.  
  588. /* Search for an execute file through our search path.
  589.    Return 0 if not found, otherwise return its name, allocated with malloc.  */
  590.  
  591. static char *
  592. find_exec_file (prog)
  593.      char *prog;
  594. {
  595.   int win = 0;
  596.   char *temp;
  597.   int size;
  598.  
  599.   size = strlen (standard_exec_prefix);
  600.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  601.     size = strlen (user_exec_prefix);
  602.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  603.     size = strlen (env_exec_prefix);
  604.   if (strlen (standard_exec_prefix_1) > size)
  605.     size = strlen (standard_exec_prefix_1);
  606.   size += strlen (prog) + 1;
  607.   if (machine_suffix)
  608.     size += strlen (machine_suffix) + 1;
  609.   temp = (char *) xmalloc (size);
  610.  
  611.   /* Determine the filename to execute.  */
  612.  
  613.   if (user_exec_prefix)
  614.     {
  615.       if (machine_suffix)
  616.     {
  617.       strcpy (temp, user_exec_prefix);
  618.       strcat (temp, machine_suffix);
  619.       strcat (temp, prog);
  620.       win = (access (temp, X_OK) == 0);
  621.     }
  622.       if (!win)
  623.     {
  624.       strcpy (temp, user_exec_prefix);
  625.       strcat (temp, prog);
  626.       win = (access (temp, X_OK) == 0);
  627.     }
  628.     }
  629.  
  630.   if (!win && env_exec_prefix)
  631.     {
  632.       if (machine_suffix)
  633.     {
  634.       strcpy (temp, env_exec_prefix);
  635.       strcat (temp, machine_suffix);
  636.       strcat (temp, prog);
  637.       win = (access (temp, X_OK) == 0);
  638.     }
  639.       if (!win)
  640.     {
  641.       strcpy (temp, env_exec_prefix);
  642.       strcat (temp, prog);
  643.       win = (access (temp, X_OK) == 0);
  644.     }
  645.     }
  646.  
  647.   if (!win)
  648.     {
  649.       if (machine_suffix)
  650.     {
  651.       strcpy (temp, standard_exec_prefix);
  652.       strcat (temp, machine_suffix);
  653.       strcat (temp, prog);
  654.       win = (access (temp, X_OK) == 0);
  655.     }
  656.       if (!win)
  657.     {
  658.       strcpy (temp, standard_exec_prefix);
  659.       strcat (temp, prog);
  660.       win = (access (temp, X_OK) == 0);
  661.     }
  662.     }
  663.  
  664.   if (!win)
  665.     {
  666.       if (machine_suffix)
  667.     {
  668.       strcpy (temp, standard_exec_prefix_1);
  669.       strcat (temp, machine_suffix);
  670.       strcat (temp, prog);
  671.       win = (access (temp, X_OK) == 0);
  672.     }
  673.       if (!win)
  674.     {
  675.       strcpy (temp, standard_exec_prefix_1);
  676.       strcat (temp, prog);
  677.       win = (access (temp, X_OK) == 0);
  678.     }
  679.     }
  680.  
  681.   if (win)
  682.     return temp;
  683.   else
  684.     return 0;
  685. }
  686.  
  687. /* stdin file number.  */
  688. #define STDIN_FILE_NO 0
  689.  
  690. /* stdout file number.  */
  691. #define STDOUT_FILE_NO 1
  692.  
  693. /* value of `pipe': port index for reading.  */
  694. #define READ_PORT 0
  695.  
  696. /* value of `pipe': port index for writing.  */
  697. #define WRITE_PORT 1
  698.  
  699. /* Pipe waiting from last process, to be used as input for the next one.
  700.    Value is STDIN_FILE_NO if no pipe is waiting
  701.    (i.e. the next command is the first of a group).  */
  702.  
  703. int last_pipe_input;
  704.  
  705. /* Fork one piped subcommand.  FUNC is the system call to use
  706.    (either execv or execvp).  ARGV is the arg vector to use.
  707.    NOT_LAST is nonzero if this is not the last subcommand
  708.    (i.e. its output should be piped to the next one.)  */
  709.  
  710. #ifdef amigados
  711.  
  712. static int
  713. pexecute (func, program, argv, not_last)
  714.      char *program;
  715.      int (*func)();
  716.      char *argv[];
  717.      int not_last;
  718. {
  719.   int pid;
  720.   int pdes[2];
  721.   int input_desc = last_pipe_input;
  722.   int output_desc = STDOUT_FILE_NO;
  723.   extern int errno;
  724.  
  725.   char *argline;
  726.   int arglinelength, i;
  727.  
  728.   for (i = 1, arglinelength=0; argv[i]; ++i)
  729.       arglinelength += strlen(argv[i]) + 1;
  730.  
  731.   arglinelength += strlen(program) + 1;
  732.  
  733.   if (!(argline = (char *)alloca(arglinelength))) 
  734.      pfatal_with_name ("alloca");
  735.  
  736.   strcpy(argline, program);
  737.   for (i = 1; argv[i]; ++i) 
  738.     {
  739.        strcat(argline, " ");
  740.        strcat(argline, argv[i]);
  741.     }
  742.  
  743.   return ssystem(argline);
  744. }
  745.  
  746. #else /* not amigados */
  747.  
  748. static int
  749. pexecute (func, program, argv, not_last)
  750.      char *program;
  751.      int (*func)();
  752.      char *argv[];
  753.      int not_last;
  754. {
  755.   int pid;
  756.   int pdes[2];
  757.   int input_desc = last_pipe_input;
  758.   int output_desc = STDOUT_FILE_NO;
  759.  
  760.   /* If this isn't the last process, make a pipe for its output,
  761.      and record it as waiting to be the input to the next process.  */
  762.  
  763.   if (not_last)
  764.     {
  765.       if (pipe (pdes) < 0)
  766.     pfatal_with_name ("pipe");
  767.       output_desc = pdes[WRITE_PORT];
  768.       last_pipe_input = pdes[READ_PORT];
  769.     }
  770.   else
  771.     last_pipe_input = STDIN_FILE_NO;
  772.  
  773.   pid = vfork ();
  774.  
  775.   switch (pid)
  776.     {
  777.     case -1:
  778.       pfatal_with_name ("vfork");
  779.       break;
  780.  
  781.     case 0: /* child */
  782.       /* Move the input and output pipes into place, if nec.  */
  783.       if (input_desc != STDIN_FILE_NO)
  784.     {
  785.       close (STDIN_FILE_NO);
  786.       dup (input_desc);
  787.       close (input_desc);
  788.     }
  789.       if (output_desc != STDOUT_FILE_NO)
  790.     {
  791.       close (STDOUT_FILE_NO);
  792.       dup (output_desc);
  793.       close (output_desc);
  794.     }
  795.  
  796.       /* Close the parent's descs that aren't wanted here.  */
  797.       if (last_pipe_input != STDIN_FILE_NO)
  798.     close (last_pipe_input);
  799.  
  800.       /* Exec the program.  */
  801.       (*func) (program, argv);
  802.       perror_exec (program);
  803.       exit (-1);
  804.       /* NOTREACHED */
  805.  
  806.     default:
  807.       /* In the parent, after forking.
  808.      Close the descriptors that we made for this child.  */
  809.       if (input_desc != STDIN_FILE_NO)
  810.     close (input_desc);
  811.       if (output_desc != STDOUT_FILE_NO)
  812.     close (output_desc);
  813.  
  814.       /* Return child's process number.  */
  815.       return pid;
  816.     }
  817. }
  818. #endif
  819.  
  820. /* Execute the command specified by the arguments on the current line of spec.
  821.    When using pipes, this includes several piped-together commands
  822.    with `|' between them.
  823.  
  824.    Return 0 if successful, -1 if failed.  */
  825.  
  826. int
  827. execute ()
  828. {
  829.   int i, j;
  830.   int n_commands;        /* # of command.  */
  831.   char *string;
  832.   struct command
  833.     {
  834.       char *prog;        /* program name.  */
  835.       char **argv;        /* vector of args.  */
  836.       int pid;            /* pid of process for this command.  */
  837.     };
  838.  
  839.   struct command *commands;    /* each command buffer with above info.  */
  840.  
  841.   /* Count # of piped commands.  */
  842.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  843.     if (strcmp (argbuf[i], "|") == 0)
  844.       n_commands++;
  845.  
  846.   /* Get storage for each command.  */
  847.   commands
  848.     = (struct command *) alloca (n_commands * sizeof (struct command));
  849.  
  850.   /* Split argbuf into its separate piped processes,
  851.      and record info about each one.
  852.      Also search for the programs that are to be run.  */
  853.  
  854.   commands[0].prog = argbuf[0]; /* first command.  */
  855.   commands[0].argv = &argbuf[0];
  856.   string = find_exec_file (commands[0].prog);
  857.   if (string)
  858.     commands[0].argv[0] = string;
  859.  
  860.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  861.     if (strcmp (argbuf[i], "|") == 0)
  862.       {                /* each command.  */
  863.     argbuf[i] = 0;    /* termination of command args.  */
  864.     commands[n_commands].prog = argbuf[i + 1];
  865.     commands[n_commands].argv = &argbuf[i + 1];
  866.     string = find_exec_file (commands[n_commands].prog);
  867.     if (string)
  868.       commands[n_commands].argv[0] = string;
  869.     n_commands++;
  870.       }
  871.  
  872.   argbuf[argbuf_index] = 0;
  873.  
  874.   /* If -v, print what we are about to do, and maybe query.  */
  875.  
  876.   if (vflag)
  877.     {
  878.       /* Print each piped command as a separate line.  */
  879.       for (i = 0; i < n_commands ; i++)
  880.     {
  881.       char **j;
  882.  
  883.       for (j = commands[i].argv; *j; j++)
  884.         fprintf (stderr, " %s", *j);
  885.  
  886.       /* Print a pipe symbol after all but the last command.  */
  887.       if (i + 1 != n_commands)
  888.         fprintf (stderr, " |");
  889.       fprintf (stderr, "\n");
  890.     }
  891.       fflush (stderr);
  892. #ifdef DEBUG
  893.       fprintf (stderr, "\nGo ahead? (y or n) ");
  894.       fflush (stderr);
  895.       j = getchar ();
  896.       if (j != '\n')
  897.     while (getchar () != '\n') ;
  898.       if (j != 'y' && j != 'Y')
  899.     return 0;
  900. #endif /* DEBUG */
  901.     }
  902.  
  903.   /* Run each piped subprocess.  */
  904.  
  905.   last_pipe_input = STDIN_FILE_NO;
  906.   for (i = 0; i < n_commands; i++)
  907.     {
  908.       extern int execv(), execvp();
  909.       char *string = commands[i].argv[0];
  910.  
  911. #ifdef amigados
  912.       commands[i].pid = pexecute (NULL,
  913.                   string, commands[i].argv,
  914.                   i + 1 < n_commands);
  915. #else
  916.       commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
  917.                   string, commands[i].argv,
  918.                   i + 1 < n_commands);
  919. #endif
  920.  
  921.       if (string != commands[i].prog)
  922.     free (string);
  923.     }
  924.  
  925.   execution_count++;
  926.  
  927.   /* Wait for all the subprocesses to finish.
  928.      We don't care what order they finish in;
  929.      we know that N_COMMANDS waits will get them all.  */
  930.  
  931.   {
  932.     int ret_code = 0;
  933.  
  934.     for (i = 0; i < n_commands; i++)
  935.       {
  936.     int status;
  937.     int pid;
  938.     char *prog;
  939.  
  940. #ifdef amigados
  941.     pid = status = commands[i].pid;
  942. #else
  943.     pid = wait (&status);
  944. #endif
  945.     if (pid < 0)
  946.       abort ();
  947.  
  948.     if (status != 0)
  949.       {
  950.         int j;
  951.         for (j = 0; j < n_commands; j++)
  952.           if (commands[j].pid == pid)
  953.         prog = commands[j].prog;
  954.  
  955.         if ((status & 0x7F) != 0)
  956.           fatal ("Program %s got fatal signal %d.", prog, (status & 0x7F));
  957.         if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  958.           ret_code = -1;
  959.       }
  960.       }
  961.     return ret_code;
  962.   }
  963. }
  964.  
  965. /* Find all the switches given to us
  966.    and make a vector describing them.
  967.    The elements of the vector a strings, one per switch given.
  968.    If a switch uses the following argument, then the `part1' field
  969.    is the switch itself and the `part2' field is the following argument.
  970.    The `valid' field is nonzero if any spec has looked at this switch;
  971.    if it remains zero at the end of the run, it must be meaningless.  */
  972.  
  973. struct switchstr
  974. {
  975.   char *part1;
  976.   char *part2;
  977.   int valid;
  978. };
  979.  
  980. struct switchstr *switches;
  981.  
  982. int n_switches;
  983.  
  984. /* Also a vector of input files specified.  */
  985.  
  986. char **infiles;
  987.  
  988. int n_infiles;
  989.  
  990. /* And a vector of corresponding output files is made up later.  */
  991.  
  992. char **outfiles;
  993.  
  994. /* Create the vector `switches' and its contents.
  995.    Store its length in `n_switches'.  */
  996.  
  997. void
  998. process_command (argc, argv)
  999.      int argc;
  1000.      char **argv;
  1001. {
  1002.   extern char *getenv ();
  1003.   register int i;
  1004.   n_switches = 0;
  1005.   n_infiles = 0;
  1006.  
  1007.   env_exec_prefix = getenv ("GCC_EXEC_PREFIX");
  1008.  
  1009.   /* Scan argv twice.  Here, the first time, just count how many switches
  1010.      there will be in their vector, and how many input files in theirs.
  1011.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  1012.  
  1013.   for (i = 1; i < argc; i++)
  1014.     {
  1015.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  1016.     {
  1017.       register char *p = &argv[i][1];
  1018.       register int c = *p;
  1019.  
  1020.       switch (c)
  1021.         {
  1022.         case 'b':
  1023.           machine_suffix = p + 1;
  1024.           break;
  1025.  
  1026.         case 'B':
  1027.           user_exec_prefix = p + 1;
  1028.           break;
  1029.  
  1030.         case 'v':    /* Print our subcommands and print versions.  */
  1031.           vflag++;
  1032.           n_switches++;
  1033.           break;
  1034.  
  1035.         default:
  1036.           n_switches++;
  1037.  
  1038.           if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  1039.         i++;
  1040.           else if (WORD_SWITCH_TAKES_ARG (p))
  1041.         i++;
  1042.         }
  1043.     }
  1044.       else
  1045.     n_infiles++;
  1046.     }
  1047.  
  1048.   /* Then create the space for the vectors and scan again.  */
  1049.  
  1050.   switches = ((struct switchstr *)
  1051.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  1052.   infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
  1053.   n_switches = 0;
  1054.   n_infiles = 0;
  1055.  
  1056.   /* This, time, copy the text of each switch and store a pointer
  1057.      to the copy in the vector of switches.
  1058.      Store all the infiles in their vector.  */
  1059.  
  1060.   for (i = 1; i < argc; i++)
  1061.     {
  1062.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  1063.     {
  1064.       register char *p = &argv[i][1];
  1065.       register int c = *p;
  1066.  
  1067.       if (c == 'B' || c == 'b')
  1068.         continue;
  1069.       switches[n_switches].part1 = p;
  1070.       if ((SWITCH_TAKES_ARG (c) && p[1] == 0)
  1071.           || WORD_SWITCH_TAKES_ARG (p))
  1072.         switches[n_switches].part2 = argv[++i];
  1073.       else if (c == 'o') {
  1074.         /* On some systems, ld cannot handle -o without space.
  1075.            So split the -o and its argument.  */
  1076.         switches[n_switches].part2 = (char *) xmalloc (strlen (p));
  1077.         strcpy (switches[n_switches].part2, &p[1]);
  1078.         p[1] = 0;
  1079.       } else
  1080.         switches[n_switches].part2 = 0;
  1081.       switches[n_switches].valid = 0;
  1082.       n_switches++;
  1083.     }
  1084.       else
  1085.     infiles[n_infiles++] = argv[i];
  1086.     }
  1087.  
  1088.   switches[n_switches].part1 = 0;
  1089.   infiles[n_infiles] = 0;
  1090. }
  1091.  
  1092. /* Process a spec string, accumulating and running commands.  */
  1093.  
  1094. /* These variables describe the input file name.
  1095.    input_file_number is the index on outfiles of this file,
  1096.    so that the output file name can be stored for later use by %o.
  1097.    input_basename is the start of the part of the input file
  1098.    sans all directory names, and basename_length is the number
  1099.    of characters starting there excluding the suffix .c or whatever.  */
  1100.  
  1101. char *input_filename;
  1102. int input_file_number;
  1103. int input_filename_length;
  1104. int basename_length;
  1105. char *input_basename;
  1106.  
  1107. /* These are variables used within do_spec and do_spec_1.  */
  1108.  
  1109. /* Nonzero if an arg has been started and not yet terminated
  1110.    (with space, tab or newline).  */
  1111. int arg_going;
  1112.  
  1113. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  1114.    is a temporary file name.  */
  1115. int delete_this_arg;
  1116.  
  1117. /* Nonzero means %w has been seen; the next arg to be terminated
  1118.    is the output file name of this compilation.  */
  1119. int this_is_output_file;
  1120.  
  1121. /* Nonzero means %s has been seen; the next arg to be terminated
  1122.    is the name of a library file and we should try the standard
  1123.    search dirs for it.  */
  1124. int this_is_library_file;
  1125.  
  1126. /* Process the spec SPEC and run the commands specified therein.
  1127.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  1128.  
  1129. int
  1130. do_spec (spec)
  1131.      char *spec;
  1132. {
  1133.   int value;
  1134.  
  1135.   clear_args ();
  1136.   arg_going = 0;
  1137.   delete_this_arg = 0;
  1138.   this_is_output_file = 0;
  1139.   this_is_library_file = 0;
  1140.  
  1141.   value = do_spec_1 (spec, 0);
  1142.  
  1143.   /* Force out any unfinished command.
  1144.      If -pipe, this forces out the last command if it ended in `|'.  */
  1145.   if (value == 0)
  1146.     {
  1147.       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1148.     argbuf_index--;
  1149.  
  1150.       if (argbuf_index > 0)
  1151.     value = execute ();
  1152.     }
  1153.  
  1154.   return value;
  1155. }
  1156.  
  1157. /* Process the sub-spec SPEC as a portion of a larger spec.
  1158.    This is like processing a whole spec except that we do
  1159.    not initialize at the beginning and we do not supply a
  1160.    newline by default at the end.
  1161.    INSWITCH nonzero means don't process %-sequences in SPEC;
  1162.    in this case, % is treated as an ordinary character.
  1163.    This is used while substituting switches.
  1164.    INSWITCH nonzero also causes SPC not to terminate an argument.
  1165.  
  1166.    Value is zero unless a line was finished
  1167.    and the command on that line reported an error.  */
  1168.  
  1169. int
  1170. do_spec_1 (spec, inswitch)
  1171.      char *spec;
  1172.      int inswitch;
  1173. {
  1174.   register char *p = spec;
  1175.   register int c;
  1176.   char *string;
  1177.  
  1178.   while (c = *p++)
  1179.     /* If substituting a switch, treat all chars like letters.
  1180.        Otherwise, NL, SPC, TAB and % are special.  */
  1181.     switch (inswitch ? 'a' : c)
  1182.       {
  1183.       case '\n':
  1184.     /* End of line: finish any pending argument,
  1185.        then run the pending command if one has been started.  */
  1186.     if (arg_going)
  1187.       {
  1188.         obstack_1grow (&obstack, 0);
  1189.         string = obstack_finish (&obstack);
  1190.         if (this_is_library_file)
  1191.           string = find_file (string);
  1192.         store_arg (string, delete_this_arg, this_is_output_file);
  1193.         if (this_is_output_file)
  1194.           outfiles[input_file_number] = string;
  1195.       }
  1196.     arg_going = 0;
  1197.  
  1198.     if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1199.       {
  1200.         int i;
  1201.         for (i = 0; i < n_switches; i++)
  1202.           if (!strcmp (switches[i].part1, "pipe"))
  1203.         break;
  1204.  
  1205.         /* A `|' before the newline means use a pipe here,
  1206.            but only if -pipe was specified.
  1207.            Otherwise, execute now and don't pass the `|' as an arg.  */
  1208.         if (i < n_switches)
  1209.           {
  1210.         switches[i].valid = 1;
  1211.         break;
  1212.           }
  1213.         else
  1214.           argbuf_index--;
  1215.       }
  1216.  
  1217.     if (argbuf_index > 0)
  1218.       {
  1219.         int value = execute ();
  1220.         if (value)
  1221.           return value;
  1222.       }
  1223.     /* Reinitialize for a new command, and for a new argument.  */
  1224.     clear_args ();
  1225.     arg_going = 0;
  1226.     delete_this_arg = 0;
  1227.     this_is_output_file = 0;
  1228.     this_is_library_file = 0;
  1229.     break;
  1230.  
  1231.       case '|':
  1232.     /* End any pending argument.  */
  1233.     if (arg_going)
  1234.       {
  1235.         obstack_1grow (&obstack, 0);
  1236.         string = obstack_finish (&obstack);
  1237.         if (this_is_library_file)
  1238.           string = find_file (string);
  1239.         store_arg (string, delete_this_arg, this_is_output_file);
  1240.         if (this_is_output_file)
  1241.           outfiles[input_file_number] = string;
  1242.       }
  1243.  
  1244.     /* Use pipe */
  1245.     obstack_1grow (&obstack, c);
  1246.     arg_going = 1;
  1247.     break;
  1248.  
  1249.       case '\t':
  1250.       case ' ':
  1251.     /* Space or tab ends an argument if one is pending.  */
  1252.     if (arg_going)
  1253.       {
  1254.         obstack_1grow (&obstack, 0);
  1255.         string = obstack_finish (&obstack);
  1256.         if (this_is_library_file)
  1257.           string = find_file (string);
  1258.         store_arg (string, delete_this_arg, this_is_output_file);
  1259.         if (this_is_output_file)
  1260.           outfiles[input_file_number] = string;
  1261.       }
  1262.     /* Reinitialize for a new argument.  */
  1263.     arg_going = 0;
  1264.     delete_this_arg = 0;
  1265.     this_is_output_file = 0;
  1266.     this_is_library_file = 0;
  1267.     break;
  1268.  
  1269.       case '%':
  1270.     switch (c = *p++)
  1271.       {
  1272.       case 0:
  1273.         fatal ("Invalid specification!  Bug in cc.");
  1274.  
  1275.       case 'b':
  1276.         obstack_grow (&obstack, input_basename, basename_length);
  1277.         arg_going = 1;
  1278.         break;
  1279.  
  1280.       case 'd':
  1281.         delete_this_arg = 2;
  1282.         break;
  1283.  
  1284.       case 'e':
  1285.         /* {...:%efoo} means report an error with `foo' as error message
  1286.            and don't execute any more commands for this file.  */
  1287.         {
  1288.           char *q = p;
  1289.           char *buf;
  1290.           while (*p != 0 && *p != '\n') p++;
  1291.           buf = (char *) alloca (p - q + 1);
  1292.           strncpy (buf, q, p - q);
  1293.           buf[p - q] = 0;
  1294.           error ("%s", buf);
  1295.           return -1;
  1296.         }
  1297.         break;
  1298.  
  1299.       case 'g':
  1300.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  1301.         delete_this_arg = 1;
  1302.         arg_going = 1;
  1303.         break;
  1304.  
  1305.       case 'i':
  1306.         obstack_grow (&obstack, input_filename, input_filename_length);
  1307.         arg_going = 1;
  1308.         break;
  1309.  
  1310.       case 'o':
  1311.         {
  1312.           register int f;
  1313.           for (f = 0; f < n_infiles; f++)
  1314.         store_arg (outfiles[f], 0, 0);
  1315.         }
  1316.         break;
  1317.  
  1318.       case 's':
  1319.         this_is_library_file = 1;
  1320.         break;
  1321.  
  1322.       case 'W':
  1323.         {
  1324.           int index = argbuf_index;
  1325.           /* Handle the {...} following the %W.  */
  1326.           if (*p != '{')
  1327.         abort ();
  1328.           p = handle_braces (p + 1);
  1329.           if (p == 0)
  1330.         return -1;
  1331.           /* If any args were output, mark the last one for deletion
  1332.          on failure.  */
  1333.           if (argbuf_index != index)
  1334.         record_temp_file (argbuf[argbuf_index - 1], 0, 1);
  1335.           break;
  1336.         }
  1337.  
  1338.       case 'w':
  1339.         this_is_output_file = 1;
  1340.         break;
  1341.  
  1342.       case '{':
  1343.         p = handle_braces (p);
  1344.         if (p == 0)
  1345.           return -1;
  1346.         break;
  1347.  
  1348.       case '%':
  1349.         obstack_1grow (&obstack, '%');
  1350.         break;
  1351.  
  1352. /*** The rest just process a certain constant string as a spec.  */
  1353.  
  1354.       case '1':
  1355.         do_spec_1 (CC1_SPEC, 0);
  1356.         break;
  1357.  
  1358.       case 'a':
  1359.         do_spec_1 (ASM_SPEC, 0);
  1360.         break;
  1361.  
  1362.       case 'c':
  1363.         do_spec_1 (SIGNED_CHAR_SPEC, 0);
  1364.         break;
  1365.  
  1366.       case 'C':
  1367.         do_spec_1 (CPP_SPEC, 0);
  1368.         break;
  1369.  
  1370.       case 'l':
  1371.         do_spec_1 (LINK_SPEC, 0);
  1372.         break;
  1373.  
  1374.       case 'L':
  1375.         do_spec_1 (LIB_SPEC, 0);
  1376.         break;
  1377.  
  1378.       case 'G':
  1379.         do_spec_1 (LIBG_SPEC, 0);
  1380.         break;
  1381.  
  1382.       case 'p':
  1383.         do_spec_1 (CPP_PREDEFINES, 0);
  1384.         break;
  1385.  
  1386.       case 'P':
  1387.         {
  1388.           char *x = (char *) alloca (strlen (CPP_PREDEFINES) * 2 + 1);
  1389.           char *buf = x;
  1390.           char *y = CPP_PREDEFINES;
  1391.  
  1392.           /* Copy all of CPP_PREDEFINES into BUF,
  1393.          but put __ after every -D and at the end of each arg,  */
  1394.           while (1)
  1395.         {
  1396.           if (! strncmp (y, "-D", 2))
  1397.             {
  1398.               *x++ = '-';
  1399.               *x++ = 'D';
  1400.               *x++ = '_';
  1401.               *x++ = '_';
  1402.               y += 2;
  1403.             }
  1404.           else if (*y == ' ' || *y == 0)
  1405.             {
  1406.               *x++ = '_';
  1407.               *x++ = '_';
  1408.               if (*y == 0)
  1409.             break;
  1410.               else
  1411.             *x++ = *y++;
  1412.             }
  1413.           else
  1414.             *x++ = *y++;
  1415.         }
  1416.           *x = 0;
  1417.  
  1418.           do_spec_1 (buf, 0);
  1419.         }
  1420.         break;
  1421.  
  1422.       case 'S':
  1423.         do_spec_1 (STARTFILE_SPEC, 0);
  1424.         break;
  1425.  
  1426.       case 'E':
  1427.         do_spec_1 (ENDFILE_SPEC, 0);
  1428.         break;
  1429.  
  1430.       default:
  1431.         abort ();
  1432.       }
  1433.     break;
  1434.  
  1435.       default:
  1436.     /* Ordinary character: put it into the current argument.  */
  1437.     obstack_1grow (&obstack, c);
  1438.     arg_going = 1;
  1439.       }
  1440.  
  1441.   return 0;        /* End of string */
  1442. }
  1443.  
  1444. /* Return 0 if we call do_spec_1 and that returns -1.  */
  1445.  
  1446. char *
  1447. handle_braces (p)
  1448.      register char *p;
  1449. {
  1450.   register char *q;
  1451.   char *filter;
  1452.   int pipe = 0;
  1453.   int negate = 0;
  1454.  
  1455.   if (*p == '|')
  1456.     /* A `|' after the open-brace means,
  1457.        if the test fails, output a single minus sign rather than nothing.
  1458.        This is used in %{|!pipe:...}.  */
  1459.     pipe = 1, ++p;
  1460.  
  1461.   if (*p == '!')
  1462.     /* A `!' after the open-brace negates the condition:
  1463.        succeed if the specified switch is not present.  */
  1464.     negate = 1, ++p;
  1465.  
  1466.   filter = p;
  1467.   while (*p != ':' && *p != '}') p++;
  1468.   if (*p != '}')
  1469.     {
  1470.       register int count = 1;
  1471.       q = p + 1;
  1472.       while (count > 0)
  1473.     {
  1474.       if (*q == '{')
  1475.         count++;
  1476.       else if (*q == '}')
  1477.         count--;
  1478.       else if (*q == 0)
  1479.         abort ();
  1480.       q++;
  1481.     }
  1482.     }
  1483.   else
  1484.     q = p + 1;
  1485.  
  1486.   if (p[-1] == '*' && p[0] == '}')
  1487.     {
  1488.       /* Substitute all matching switches as separate args.  */
  1489.       register int i;
  1490.       --p;
  1491.       for (i = 0; i < n_switches; i++)
  1492.     if (!strncmp (switches[i].part1, filter, p - filter))
  1493.       give_switch (i);
  1494.     }
  1495.   else
  1496.     {
  1497.       /* Test for presence of the specified switch.  */
  1498.       register int i;
  1499.       int present = 0;
  1500.  
  1501.       /* If name specified ends in *, as in {x*:...},
  1502.      check for presence of any switch name starting with x.  */
  1503.       if (p[-1] == '*')
  1504.     {
  1505.       for (i = 0; i < n_switches; i++)
  1506.         {
  1507.           if (!strncmp (switches[i].part1, filter, p - filter - 1))
  1508.         {
  1509.           switches[i].valid = 1;
  1510.           present = 1;
  1511.         }
  1512.         }
  1513.     }
  1514.       /* Otherwise, check for presence of exact name specified.  */
  1515.       else
  1516.     {
  1517.       for (i = 0; i < n_switches; i++)
  1518.         {
  1519.           if (!strncmp (switches[i].part1, filter, p - filter)
  1520.           && switches[i].part1[p - filter] == 0)
  1521.         {
  1522.           switches[i].valid = 1;
  1523.           present = 1;
  1524.           break;
  1525.         }
  1526.         }
  1527.     }
  1528.  
  1529.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  1530.      then substitute either the switch or the specified
  1531.      conditional text.  */
  1532.       if (present != negate)
  1533.     {
  1534.       if (*p == '}')
  1535.         {
  1536.           give_switch (i);
  1537.         }
  1538.       else
  1539.         {
  1540.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
  1541.         return 0;
  1542.         }
  1543.     }
  1544.       else if (pipe)
  1545.     {
  1546.       /* Here if a %{|...} conditional fails: output a minus sign,
  1547.          which means "standard output" or "standard input".  */
  1548.       do_spec_1 ("-", 0);
  1549.     }
  1550.     }
  1551.  
  1552.   return q;
  1553. }
  1554.  
  1555. /* Pass a switch to the current accumulating command
  1556.    in the same form that we received it.
  1557.    SWITCHNUM identifies the switch; it is an index into
  1558.    the vector of switches gcc received, which is `switches'.
  1559.    This cannot fail since it never finishes a command line.  */
  1560.  
  1561. give_switch (switchnum)
  1562.      int switchnum;
  1563. {
  1564.   do_spec_1 ("-", 0);
  1565.   do_spec_1 (switches[switchnum].part1, 1);
  1566.   do_spec_1 (" ", 0);
  1567.   if (switches[switchnum].part2 != 0)
  1568.     {
  1569.       do_spec_1 (switches[switchnum].part2, 1);
  1570.       do_spec_1 (" ", 0);
  1571.     }
  1572.   switches[switchnum].valid = 1;
  1573. }
  1574.  
  1575. /* Search for a file named NAME trying various prefixes including the
  1576.    user's -B prefix and some standard ones.
  1577.    Return the absolute pathname found.  If nothing is found, return NAME.  */
  1578.  
  1579. char *
  1580. find_file (name)
  1581.      char *name;
  1582. {
  1583.   int size;
  1584.   char *temp;
  1585.   int win = 0;
  1586.  
  1587.   /* Compute maximum size of NAME plus any prefix we will try.  */
  1588.  
  1589.   size = strlen (standard_exec_prefix);
  1590.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  1591.     size = strlen (user_exec_prefix);
  1592.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  1593.     size = strlen (env_exec_prefix);
  1594.   if (strlen (standard_exec_prefix) > size)
  1595.     size = strlen (standard_exec_prefix);
  1596.   if (strlen (standard_exec_prefix_1) > size)
  1597.     size = strlen (standard_exec_prefix_1);
  1598.   if (strlen (standard_startfile_prefix) > size)
  1599.     size = strlen (standard_startfile_prefix);
  1600.   if (strlen (standard_startfile_prefix_1) > size)
  1601.     size = strlen (standard_startfile_prefix_1);
  1602.   if (strlen (standard_startfile_prefix_2) > size)
  1603.     size = strlen (standard_startfile_prefix_2);
  1604.   if (machine_suffix)
  1605.     size += strlen (machine_suffix) + 1;
  1606.   size += strlen (name) + 1;
  1607.  
  1608.   temp = (char *) alloca (size);
  1609.  
  1610.   if (user_exec_prefix)
  1611.     {
  1612.       if (machine_suffix)
  1613.     {
  1614.       strcpy (temp, user_exec_prefix);
  1615.       strcat (temp, machine_suffix);
  1616.       strcat (temp, name);
  1617.       win = (access (temp, R_OK) == 0);
  1618.     }
  1619.       if (!win)
  1620.     {
  1621.       strcpy (temp, user_exec_prefix);
  1622.       strcat (temp, name);
  1623.       win = (access (temp, R_OK) == 0);
  1624.     }
  1625.     }
  1626.  
  1627.   if (!win && env_exec_prefix)
  1628.     {
  1629.       if (machine_suffix)
  1630.     {
  1631.       strcpy (temp, env_exec_prefix);
  1632.       strcat (temp, machine_suffix);
  1633.       strcat (temp, name);
  1634.       win = (access (temp, R_OK) == 0);
  1635.     }
  1636.       if (!win)
  1637.     {
  1638.       strcpy (temp, env_exec_prefix);
  1639.       strcat (temp, name);
  1640.       win = (access (temp, R_OK) == 0);
  1641.     }
  1642.     }
  1643.  
  1644.   if (!win)
  1645.     {
  1646.       if (machine_suffix)
  1647.     {
  1648.       strcpy (temp, standard_exec_prefix);
  1649.       strcat (temp, machine_suffix);
  1650.       strcat (temp, name);
  1651.       win = (access (temp, R_OK) == 0);
  1652.     }
  1653.       if (!win)
  1654.     {
  1655.       strcpy (temp, standard_exec_prefix);
  1656.       strcat (temp, name);
  1657.       win = (access (temp, R_OK) == 0);
  1658.     }
  1659.     }
  1660.  
  1661.   if (!win)
  1662.     {
  1663.       if (machine_suffix)
  1664.     {
  1665.       strcpy (temp, standard_exec_prefix_1);
  1666.       strcat (temp, machine_suffix);
  1667.       strcat (temp, name);
  1668.       win = (access (temp, R_OK) == 0);
  1669.     }
  1670.       if (!win)
  1671.     {
  1672.       strcpy (temp, standard_exec_prefix_1);
  1673.       strcat (temp, name);
  1674.       win = (access (temp, R_OK) == 0);
  1675.     }
  1676.     }
  1677.  
  1678.   if (!win)
  1679.     {
  1680.       if (machine_suffix)
  1681.     {
  1682.       strcpy (temp, standard_startfile_prefix);
  1683.       strcat (temp, machine_suffix);
  1684.       strcat (temp, name);
  1685.       win = (access (temp, R_OK) == 0);
  1686.     }
  1687.       if (!win)
  1688.     {
  1689.       strcpy (temp, standard_startfile_prefix);
  1690.       strcat (temp, name);
  1691.       win = (access (temp, R_OK) == 0);
  1692.     }
  1693.     }
  1694.  
  1695.   if (!win)
  1696.     {
  1697.       if (machine_suffix)
  1698.     {
  1699.       strcpy (temp, standard_startfile_prefix_1);
  1700.       strcat (temp, machine_suffix);
  1701.       strcat (temp, name);
  1702.       win = (access (temp, R_OK) == 0);
  1703.     }
  1704.       if (!win)
  1705.     {
  1706.       strcpy (temp, standard_startfile_prefix_1);
  1707.       strcat (temp, name);
  1708.       win = (access (temp, R_OK) == 0);
  1709.     }
  1710.     }
  1711.  
  1712.   if (!win)
  1713.     {
  1714.       if (machine_suffix)
  1715.     {
  1716.       strcpy (temp, standard_startfile_prefix_2);
  1717.       strcat (temp, machine_suffix);
  1718.       strcat (temp, name);
  1719.       win = (access (temp, R_OK) == 0);
  1720.     }
  1721.       if (!win)
  1722.     {
  1723.       strcpy (temp, standard_startfile_prefix_2);
  1724.       strcat (temp, name);
  1725.       win = (access (temp, R_OK) == 0);
  1726.     }
  1727.     }
  1728.  
  1729.   if (!win)
  1730.     {
  1731.       if (machine_suffix)
  1732.     {
  1733.       strcpy (temp, "./");
  1734.       strcat (temp, machine_suffix);
  1735.       strcat (temp, name);
  1736.       win = (access (temp, R_OK) == 0);
  1737.     }
  1738.       if (!win)
  1739.     {
  1740.       strcpy (temp, "./");
  1741.       strcat (temp, name);
  1742.       win = (access (temp, R_OK) == 0);
  1743.     }
  1744.     }
  1745.  
  1746.   if (win)
  1747.     return save_string (temp, strlen (temp));
  1748.   return name;
  1749. }
  1750.  
  1751. /* On fatal signals, delete all the temporary files.  */
  1752.  
  1753. void
  1754. fatal_error (signum)
  1755.      int signum;
  1756. {
  1757.   signal (signum, SIG_DFL);
  1758.   delete_failure_queue ();
  1759.   delete_temp_files ();
  1760.   /* Get the same signal again, this time not handled,
  1761.      so its normal effect occurs.  */
  1762.   kill (getpid (), signum);
  1763. }
  1764.  
  1765. int
  1766. main (argc, argv)
  1767.      int argc;
  1768.      char **argv;
  1769. {
  1770.   register int i;
  1771.   int value;
  1772.   int error_count = 0;
  1773.   int linker_was_run = 0;
  1774.   char *explicit_link_files;
  1775.  
  1776.   programname = argv[0];
  1777.  
  1778.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  1779.     signal (SIGINT, fatal_error);
  1780.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  1781.     signal (SIGHUP, fatal_error);
  1782.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  1783.     signal (SIGTERM, fatal_error);
  1784.   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
  1785.     signal (SIGPIPE, fatal_error);
  1786.  
  1787.   argbuf_length = 10;
  1788.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  1789.  
  1790.   obstack_init (&obstack);
  1791.  
  1792.   choose_temp_base ();
  1793.  
  1794.   /* Make a table of what switches there are (switches, n_switches).
  1795.      Make a table of specified input files (infiles, n_infiles).  */
  1796.  
  1797.   process_command (argc, argv);
  1798.  
  1799.   if (vflag)
  1800.     {
  1801.       extern char *version_string;
  1802.       fprintf (stderr, "gcc version %s\n", version_string);
  1803.       if (n_infiles == 0)
  1804.     exit (0);
  1805.     }
  1806.  
  1807.   if (n_infiles == 0)
  1808.     fatal ("No input files specified.");
  1809.  
  1810.   /* Make a place to record the compiler output file names
  1811.      that correspond to the input files.  */
  1812.  
  1813.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  1814.   bzero (outfiles, n_infiles * sizeof (char *));
  1815.  
  1816.   /* Record which files were specified explicitly as link input.  */
  1817.  
  1818.   explicit_link_files = (char *) xmalloc (n_infiles);
  1819.   bzero (explicit_link_files, n_infiles);
  1820.  
  1821.   for (i = 0; i < n_infiles; i++)
  1822.     {
  1823.       register struct compiler *cp;
  1824.       int this_file_error = 0;
  1825.  
  1826.       /* Tell do_spec what to substitute for %i.  */
  1827.  
  1828.       input_filename = infiles[i];
  1829.       input_filename_length = strlen (input_filename);
  1830.       input_file_number = i;
  1831.  
  1832.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  1833.  
  1834.       outfiles[i] = input_filename;
  1835.  
  1836.       /* Figure out which compiler from the file's suffix.  */
  1837.  
  1838.       for (cp = compilers; cp->spec; cp++)
  1839.     {
  1840.       if (strlen (cp->suffix) < input_filename_length
  1841.           && !strcmp (cp->suffix,
  1842.               infiles[i] + input_filename_length
  1843.               - strlen (cp->suffix)))
  1844.         {
  1845.           /* Ok, we found an applicable compiler.  Run its spec.  */
  1846.           /* First say how much of input_filename to substitute for %b  */
  1847.           register char *p;
  1848.  
  1849.           input_basename = input_filename;
  1850.           for (p = input_filename; *p; p++)
  1851.         if (*p == '/')
  1852.           input_basename = p + 1;
  1853. #ifdef amigados
  1854.         else if (index (p, ':'))
  1855.           input_basename = index (p, ':') + 1;
  1856. #endif
  1857.           basename_length = (input_filename_length - strlen (cp->suffix)
  1858.                  - (input_basename - input_filename));
  1859.           value = do_spec (cp->spec);
  1860.           if (value < 0)
  1861.         this_file_error = 1;
  1862.           break;
  1863.         }
  1864.     }
  1865.  
  1866.       /* If this file's name does not contain a recognized suffix,
  1867.      record it as explicit linker input.  */
  1868.  
  1869.       if (! cp->spec)
  1870.     explicit_link_files[i] = 1;
  1871.  
  1872.       /* Clear the delete-on-failure queue, deleting the files in it
  1873.      if this compilation failed.  */
  1874.  
  1875.       if (this_file_error)
  1876.     {
  1877.       delete_failure_queue ();
  1878.       error_count++;
  1879.     }
  1880.       /* If this compilation succeeded, don't delete those files later.  */
  1881.       clear_failure_queue ();
  1882.     }
  1883.  
  1884.   /* Run ld to link all the compiler output files.  */
  1885.  
  1886.   if (error_count == 0)
  1887.     {
  1888.       int tmp = execution_count;
  1889.       value = do_spec (link_spec);
  1890.       if (value < 0)
  1891.     error_count = 1;
  1892.       linker_was_run = (tmp != execution_count);
  1893.     }
  1894.  
  1895.   /* If options said don't run linker,
  1896.      complain about input files to be given to the linker.  */
  1897.  
  1898.   if (! linker_was_run && error_count == 0)
  1899.     for (i = 0; i < n_infiles; i++)
  1900.       if (explicit_link_files[i])
  1901.     error ("%s: linker input file unused since linking not done",
  1902.            outfiles[i]);
  1903.  
  1904.   /* Set the `valid' bits for switches that match anything in any spec.  */
  1905.  
  1906.   validate_all_switches ();
  1907.  
  1908.   /* Warn about any switches that no pass was interested in.  */
  1909.   
  1910.   for (i = 0; i < n_switches; i++)
  1911.     if (! switches[i].valid)
  1912.       error ("unrecognized option `-%s'", switches[i].part1);
  1913.  
  1914.   /* Delete some or all of the temporary files we made.  */
  1915.  
  1916.   if (error_count)
  1917.     delete_failure_queue ();
  1918.   delete_temp_files ();
  1919.  
  1920.   exit (error_count);
  1921. }
  1922.  
  1923. xmalloc (size)
  1924.      int size;
  1925. {
  1926.   register int value = malloc (size);
  1927.   if (value == 0)
  1928.     fatal ("virtual memory exhausted");
  1929.   return value;
  1930. }
  1931.  
  1932. xrealloc (ptr, size)
  1933.      int ptr, size;
  1934. {
  1935.   register int value = realloc (ptr, size);
  1936.   if (value == 0)
  1937.     fatal ("virtual memory exhausted");
  1938.   return value;
  1939. }
  1940.  
  1941. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  1942.  
  1943. char *
  1944. concat (s1, s2, s3)
  1945.      char *s1, *s2, *s3;
  1946. {
  1947.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1948.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1949.  
  1950.   strcpy (result, s1);
  1951.   strcpy (result + len1, s2);
  1952.   strcpy (result + len1 + len2, s3);
  1953.   *(result + len1 + len2 + len3) = 0;
  1954.  
  1955.   return result;
  1956. }
  1957.  
  1958. char *
  1959. save_string (s, len)
  1960.      char *s;
  1961.      int len;
  1962. {
  1963.   register char *result = (char *) xmalloc (len + 1);
  1964.  
  1965.   bcopy (s, result, len);
  1966.   result[len] = 0;
  1967.   return result;
  1968. }
  1969.  
  1970. pfatal_with_name (name)
  1971.      char *name;
  1972. {
  1973.   extern int errno, sys_nerr;
  1974.   char *s;
  1975.  
  1976.   if (errno < sys_nerr)
  1977.     s = concat ("%s: ", strerror (errno), "");
  1978.   else
  1979.     s = "cannot open %s";
  1980.   fatal (s, name);
  1981. }
  1982.  
  1983. perror_with_name (name)
  1984.      char *name;
  1985. {
  1986.   extern int errno, sys_nerr;
  1987.   char *s;
  1988.  
  1989.   if (errno < sys_nerr)
  1990.     s = concat ("%s: ", strerror (errno), "");
  1991.   else
  1992.     s = "cannot open %s";
  1993.   error (s, name);
  1994. }
  1995.  
  1996. perror_exec (name)
  1997.      char *name;
  1998. {
  1999.   extern int errno, sys_nerr;
  2000.   char *s;
  2001.  
  2002.   if (errno < sys_nerr)
  2003.     s = concat ("installation problem, cannot exec %s: ",
  2004.         strerror (errno), "");
  2005.   else
  2006.     s = "installation problem, cannot exec %s";
  2007.   error (s, name);
  2008. }
  2009.  
  2010. /* More 'friendly' abort that prints the line and file.
  2011.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  2012.  
  2013. void
  2014. fancy_abort ()
  2015. {
  2016.   fatal ("Internal gcc abort.");
  2017. }
  2018.  
  2019. #ifdef HAVE_VPRINTF
  2020.  
  2021. /* Output an error message and exit */
  2022.  
  2023. int 
  2024. fatal (va_alist)
  2025.      va_dcl
  2026. {
  2027.   va_list ap;
  2028.   char *format;
  2029.   
  2030.   va_start(ap);
  2031.   format = va_arg (ap, char *);
  2032.   vfprintf (stderr, format, ap);
  2033.   va_end (ap);
  2034.   fprintf (stderr, "\n");
  2035.   delete_temp_files ();
  2036.   exit (1);
  2037. }  
  2038.  
  2039. error (va_alist) 
  2040.      va_dcl
  2041. {
  2042.   va_list ap;
  2043.   char *format;
  2044.  
  2045.   va_start(ap);
  2046.   format = va_arg (ap, char *);
  2047.   fprintf (stderr, "%s: ", programname);
  2048.   vfprintf (stderr, format, ap);
  2049.   va_end (ap);
  2050.  
  2051.   fprintf (stderr, "\n");
  2052. }
  2053.  
  2054. #else /* not HAVE_VPRINTF */
  2055.  
  2056. fatal (msg, arg1, arg2)
  2057.      char *msg, *arg1, *arg2;
  2058. {
  2059.   error (msg, arg1, arg2);
  2060.   delete_temp_files (0);
  2061.   exit (1);
  2062. }
  2063.  
  2064. error (msg, arg1, arg2)
  2065.      char *msg, *arg1, *arg2;
  2066. {
  2067.   fprintf (stderr, "%s: ", programname);
  2068.   fprintf (stderr, msg, arg1, arg2);
  2069.   fprintf (stderr, "\n");
  2070. }
  2071.  
  2072. #endif /* not HAVE_VPRINTF */
  2073.  
  2074.  
  2075. void
  2076. validate_all_switches ()
  2077. {
  2078.   struct compiler *comp;
  2079.   register char *p;
  2080.   register char c;
  2081.  
  2082.   for (comp = compilers; comp->spec; comp++)
  2083.     {
  2084.       p = comp->spec;
  2085.       while (c = *p++)
  2086.     if (c == '%' && *p == '{')
  2087.       /* We have a switch spec.  */
  2088.       validate_switches (p + 1);
  2089.     }
  2090.  
  2091.   p = link_spec;
  2092.   while (c = *p++)
  2093.     if (c == '%' && *p == '{')
  2094.       /* We have a switch spec.  */
  2095.       validate_switches (p + 1);
  2096.  
  2097.   /* Now notice switches mentioned in the machine-specific specs.  */
  2098.  
  2099. #ifdef ASM_SPEC
  2100.   p = ASM_SPEC;
  2101.   while (c = *p++)
  2102.     if (c == '%' && *p == '{')
  2103.       /* We have a switch spec.  */
  2104.       validate_switches (p + 1);
  2105. #endif
  2106.  
  2107. #ifdef CPP_SPEC
  2108.   p = CPP_SPEC;
  2109.   while (c = *p++)
  2110.     if (c == '%' && *p == '{')
  2111.       /* We have a switch spec.  */
  2112.       validate_switches (p + 1);
  2113. #endif
  2114.  
  2115. #ifdef SIGNED_CHAR_SPEC
  2116.   p = SIGNED_CHAR_SPEC;
  2117.   while (c = *p++)
  2118.     if (c == '%' && *p == '{')
  2119.       /* We have a switch spec.  */
  2120.       validate_switches (p + 1);
  2121. #endif
  2122.  
  2123. #ifdef CC1_SPEC
  2124.   p = CC1_SPEC;
  2125.   while (c = *p++)
  2126.     if (c == '%' && *p == '{')
  2127.       /* We have a switch spec.  */
  2128.       validate_switches (p + 1);
  2129. #endif
  2130.  
  2131. #ifdef LINK_SPEC
  2132.   p = LINK_SPEC;
  2133.   while (c = *p++)
  2134.     if (c == '%' && *p == '{')
  2135.       /* We have a switch spec.  */
  2136.       validate_switches (p + 1);
  2137. #endif
  2138.  
  2139. #ifdef LIB_SPEC
  2140.   p = LIB_SPEC;
  2141.   while (c = *p++)
  2142.     if (c == '%' && *p == '{')
  2143.       /* We have a switch spec.  */
  2144.       validate_switches (p + 1);
  2145. #endif
  2146.  
  2147. #ifdef STARTFILE_SPEC
  2148.   p = STARTFILE_SPEC;
  2149.   while (c = *p++)
  2150.     if (c == '%' && *p == '{')
  2151.       /* We have a switch spec.  */
  2152.       validate_switches (p + 1);
  2153. #endif
  2154. }
  2155.  
  2156. /* Look at the switch-name that comes after START
  2157.    and mark as valid all supplied switches that match it.  */
  2158.  
  2159. void
  2160. validate_switches (start)
  2161.      char *start;
  2162. {
  2163.   register char *p = start;
  2164.   char *filter;
  2165.   register int i;
  2166.  
  2167.   if (*p == '|')
  2168.     ++p;
  2169.  
  2170.   if (*p == '!')
  2171.     ++p;
  2172.  
  2173.   filter = p;
  2174.   while (*p != ':' && *p != '}') p++;
  2175.  
  2176.   if (p[-1] == '*')
  2177.     {
  2178.       /* Mark all matching switches as valid.  */
  2179.       --p;
  2180.       for (i = 0; i < n_switches; i++)
  2181.     if (!strncmp (switches[i].part1, filter, p - filter))
  2182.       switches[i].valid = 1;
  2183.     }
  2184.   else
  2185.     {
  2186.       /* Mark an exact matching switch as valid.  */
  2187.       for (i = 0; i < n_switches; i++)
  2188.     {
  2189.       if (!strncmp (switches[i].part1, filter, p - filter)
  2190.           && switches[i].part1[p - filter] == 0)
  2191.         switches[i].valid = 1;
  2192.     }
  2193.     }
  2194. }
  2195.